<?php
if(isset($_POST['del']) && !empty($_POST['del'])){
    $del_arr = $_POST['del'];
    $del_ids = implode(",", $del_arr);
    $rs = getdb( "select id from lotto where id in ($del_ids) and amount=0");//传入id是否存在下单金额不为0判断--不可删除的ID
    $ret = array();
    while (!$rs->eof){
        $ret[] = $rs->f['id'];
        $rs->movenext();
    }
    $end_arr = array_unique($ret);
    $end_ids = implode(",", $end_arr);
    $success_count = count($end_arr);
    $failed_count = count($del_arr) - $success_count;
    getdb("DELETE FROM lotto WHERE id in ($end_ids)");
    echo "<script>alert('删除成功".$success_count."条，删除失败".$failed_count."条！');window.location='op.php?op=admin&fp=history'</script>";
}else if(isset($_POST['del'])){
    echo "<script>alert('删除失败，请指定删除条目！');window.location='op.php?op=admin&fp=history'</script>";
}
function num2img( $num )
{
    $color = array( "", "red", "blue", "green" );
    if ( intval( $num ) == 0 )
    {
        return "&nbsp;";
    }
    return "&nbsp;<b><font color=".$color[check_color( $num )].">".d2( $num )."</font></b>&nbsp;";
}

function current_lottoid( )
{
    $rs = getdb( "select id, open from lotto where open>='".date( "Y-m-d" )."' order by open" );
    if ( $rs->eof )
    {
        return false;
    }
    return $rs->f['id'];
}

if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
$ui = new UI();
$conf = new Config();
$tpl = new template2();
if ( isset( $_GET['calc'] ) )
{
    $calcbet = intval( $_GET['calc'] );
    if ( $calcbet == 1 )
    {
        $conf->set( "calcbet", 0 );
    }
}
$tpl->load( "main", "history.htm" );
$add = "";
if ( $conf->get( "calcbet" ) == 1 )
{
    $msg = title( "开奖管理", $add );
    $ui->showErrorPage( $msg."<p class=error>颁奖过帐中，请稍後再使用本功能！</p>" );
    exit();
}
$yy = intval( $conf->get( "page_max_count_ag" ) );
if ( $curruser['prev_id'] == 0 || SubAccountRight( $curruser, 3 ) == 2 )
{
    $add = "<a href=op.php?op=admin&fp=lotto&cmd=add>新增</a>";
    $tpl->set( "updivSave", "<input type=\"button\" value=\"储存\" id=\"updivSave\" />" );
    $tpl->set( "td1", "<td>赔率</td><td>备注</td>" );
}
else
{
    $tpl->set( "td1", "<td>备注</td>" );
}
echo title( "开奖管理", $add );
echo "<p>";
$weekname1 = array( "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" );
$weekname2 = array( "日", "一", "二", "三", "四", "五", "六" );
$lottoid = current_lottoid();
$paperShowOpen = getPaperMinDate( 0 - 1 );
$list = "";
$page = intval( getvar( "p" ) );
if ( $page == 0 )
{
    $page = 1;
}
$rs = getdb( "select * from lotto order by open desc", $page, 15 );
$a0 = 0;
$a1 = 0;
$a2 = 0;
$a6 = 0;
$a7 = 0;
$a8 = 0;
$ii = 0;
while ( !$rs->eof )
{
    $id = $rs->f['id'];
    $hrefmod = $lottoid == $id;
    if ( $curruser['account'] == G_SUPERACCOUNT && $curruser['acl'] == 255 )
    {
        $hrefmod = true;
    }
    $list .= "<tr class=td".( $lottoid == $id ? 6 : 2 ).">";
    $list .= "<td>";
    if ( 0 < $curruser['prev_id'] && SubAccountRight( $curruser, 3 ) < 2 || !$hrefmod && $rs->f['spec'] != 0 )
    {
        $list .= $rs->f['open'];
    }
    else
    {
        $list .= "<a href=\"?op=admin&fp=lotto&cmd=edit&id=".$id."\">".$rs->f['open']."</a>";
    }
    $open = $rs->f['open'];
    $week = date( "w", strtotime( $rs->f['open'] ) );
    $list .= "<td align=center>".$weekname2[$week];
    $list .= "<td align=right>".d3( $rs->f['round'] );
    $list .= "<td>";
    $i = 1;
    while ( $i <= 6 )
    {
        $list .= num2img( $rs->f["n".$i] );
        ++$i;
    }
    $list .= "<td align=center>".num2img( $rs->f['spec'] );
    $resultg = 0;
    $result0 = 0;
    $result1 = 0;
    $result2 = 0;
    $result6 = 0;
    $result7 = 0;
    $result8 = 0;
    if ( date( "Y-m" ) == substr( $open, 0, 7 ) && $rs->f['rsum'] == 0 )
    {
        $rs1 = getdb( "select sum(amount),sum(b1),sum(r5),sum(g5) from bet where lotto_id=".$id." and rame in (0,9)" );
        if ( !$rs1->eof )
        {
            $result0 = $rs1->f[0] + 0;
            $result1 = $rs1->f[1] + 0;
            $result2 = $rs1->f[2] + 0;
            $resultg = $rs1->f[3] + 0;
        }
        $rs2 = getdb( "select sum(r5),sum(g5) from bet2 where lotto_id=".$id." and rame in (0,9)" );
        if ( !$rs2->eof )
        {
            $result2 += $rs2->f[0] + 0;
            $resultg += $rs2->f[1] + 0;
        }
        $rs3 = getdb( "select sum(amount),sum(r5) from bet8 where lotto_id=".$id." and rame in (0,9)" );
        $result6 = $rs3->f[0] + 0;
        $result7 = $rs3->f[1] + 0;
        $result8 = $result2 + $result7;
        if ( 0 < $result0 || 0 < $result6 )
        {
            getdb( "update lotto set amount={$result0}, mb={$result1}, ad={$result2}, ua={$result6}, ur={$result7}, rsum={$result8} where id=".$id );
        }
    }
    else if ( $ii < $yy && $paperShowOpen <= $open )
    {
        $result0 = $rs->f['amount'];
        $result1 = $rs->f['mb'];
        $result2 = $rs->f['ad'];
        $result6 = $rs->f['ua'];
        $result7 = $rs->f['ur'];
        $result8 = $rs->f['rsum'];
    }
    ++$ii;
    $list .= "<td align=right> ";
    if ( $hrefmod && 0 < $result0 )
    {
        $list .= "<a href='?op=agent&fp=betlist&dt1=".$open."&dt2=".$open."'>";
    }
    $list .= sprintf( "%.0f", $result0 );
    if ( $hrefmod && 0 < $result0 )
    {
        $list .= "</a>";
    }
    $list .= "<td align=right> ".say_numbercolor( $result1 );
    $list .= "<td align=right> ".say_numbercolor( $result2 - $resultg );
    $list .= "<td align=right> ";
    $sql = " bet8 where lotto_id=".$id;
    if ( $hrefmod && 0 < $result6 )
    {
        $list .= "<a href='?op=agent&fp=bet8list&c=".$sql."'>";
    }
    $list .= sprintf( "%.0f", $result6 );
    if ( $hrefmod && 0 < $result6 )
    {
        $list .= "</a>";
    }
    $list .= "<td align=right> ".say_numbercolor( $result7 );
    $list .= "<td align=right> ".say_numbercolor( $result8 - $resultg );
    if ( $curruser['prev_id'] == 0 || SubAccountRight( $curruser, 3 ) == 2 )
    {
        if ( date( "Y-m" ) <= substr( $open, 0, 7 ) )
        {
            $list .= "<td>&nbsp;<a href=\"op.php?op=admin&fp=rate1&id=".$rs->f['id']."\">赔率</a>&nbsp;</td>";
        }
        else
        {
            $list .= "<td>&nbsp;&nbsp;</td>";
        }
    }
    $_okMsg = $rs->f['note'] == "" ? "" : "_ok";
    $list .= "<td nowrap><a href=\"#".$rs->f['id']."_".$rs->f['round']."\" class=\"editNote\" ><img src=\"image/edit{$_okMsg}.png\" width=\"16\" border=\"0\"></a></td>";
    $list .="<td align=center><input type='checkbox' name='del[]' value='".$id."' /></td>";
    $list .= "</tr>";
    $a0 += $result0;
    $a1 += $result1;
    $a2 += $result2 - $resultg;
    $a6 += $result6;
    $a7 += $result7;
    $a8 += $result8 - $resultg;
    $rs->movenext();
}
$list .= "<tr class=td0 align=right>";
$list .= "<td colspan=5 align=right><b>总计</b><td><b>".$a0."</b><td><b>".say_numbercolor( $a1 )."</b><td><b>".say_numbercolor( $a2 )."</b><td><b>".$a6."</b><td><b>".say_numbercolor( $a7 )."</b><td><b>".say_numbercolor( $a8 )."</b><td></td><td></td><td> <input type=\"hidden\" name=\"op\" value=\"admin\"/><input type=\"hidden\" name=\"fp\" value=\"history\"/> <input type=\"submit\" value=\"删除\"/></td>";
$list .= "</tr></table>";
$rs->close();
$url = "op.php?op=admin&fp=history&p=";
if ( $page == 1 )
{
    $button = "第一页 &nbsp; 上一页 &nbsp; <a href=\"".$url."2\">下一页</a> &nbsp; <a href=\"".$url.$rs->totalpages."\">最後页</a>";
}
else if ( $page == $rs->totalpages )
{
    $button = "<a href=\"".$url."1\">第一页</a> &nbsp; <a href=\"".$url.( $page - 1 )."\">上一页</a> &nbsp; 下一页 &nbsp; 最後页";
}
else
{
    $button = "<a href=\"".$url."1\">第一页</a> &nbsp; <a href=\"".$url.( $page - 1 )."\">上一页</a> &nbsp; <a href=\"".$url."2\">下一页</a> &nbsp; <a href=\"".$url.$rs->totalpages."\">最後页</a>";
}
$button .= " &#12288;&#12288;&#12288;&#12288;&#12288;&#12288;&#12288;&#12288; <font color=red>※ 本表为概略计算仅供参考，准确数据一律以报表为准。</font><br>";
$tpl->set( "button", $button );
$tpl->set( "list", $list );
$tpl->show( "main" );
$ui->footer();
?>